﻿using Newtonsoft.Json.Linq;
using System;

using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace xDataSet
{
    public class RedisHelper
    {
        public static void test()
        {
            Task task = new Task(() =>
            {
                StackExchangeRedisHelper.SetString(10, "nihao", "world");
            });
            task.Start();
        }



        public static JObject excuteOperation(JObject ob)
        {
            JObject retvalue = new JObject();
            retvalue["CODE"] = 1;
            int db;
            string command = "";
            string key = "";
            string value = "";
            int expire = 0;//单位 分
            if (ob["DBNO"] != null)
            {
                db = Convert.ToInt32(ob["DBNO"]);
            }
            else
            {
                throw new Exception("未指定数据库编号。");
            }

            if (ob["COMMAND"] != null)
            {
                command = ob["COMMAND"].ToString();
            }
            else
            {
                throw new Exception("未指定命令类型。");
            }

            if (ob["EXPIRE"] != null)
            {
                expire = Convert.ToInt32(ob["EXPIRE"]);
            }
            if (ob["KEY"] != null)
            {
                key = ob["KEY"].ToString();
            }
            if (ob["VALUE"] != null)
            {
                value = ob["VALUE"].ToString();
            }

            if (command.Equals("CLEAN"))
            {
                StackExchangeRedisHelper.FlushWipeDataBase(db);
                return retvalue;
            }
            else if (command.Equals("REMOVE"))
            {
                StackExchangeRedisHelper.DeleteByKey(db, key);
                return retvalue;
            }
            else if (command.Equals("ADD"))
            {
                if (expire == 0)
                { StackExchangeRedisHelper.SetString(db, key, value, 60); }
                else if (expire > 0)
                { StackExchangeRedisHelper.SetString(db, key, value, expire); }
                return retvalue;
            }
            else if (command.Equals("GET"))
            {
                var result = StackExchangeRedisHelper.GetString(db, key);
                if (result == null)
                {
                    retvalue["DATA"] = "null";
                }
                else
                {
                    retvalue["DATA"] = result;
                }
                return retvalue;
            }
            return retvalue;
        }
        /// <summary>
        /// 插入一个用户到redis数据库
        /// </summary>
        /// <param name="entity"></param>
        //public static void f_InsertUser(UserInfoEntity entity)
        //{
        //    Task task = new Task(() =>
        //    {
        //        insertUser(entity);
        //    });
        //    task.Start();
        //}
        /// <summary>
        /// 根据用户编码获取一个用户的信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        //public static UserInfoEntity f_GetUserByUserCode(string code)
        //{
        //    try
        //    {
        //        string key = "User:" + code;
        //        string value = StackExchangeRedisHelper.GetString(Config.RedisDBNo, key);
        //        UserInfoEntity entity = Deserialize<UserInfoEntity>(value);
        //        return entity;
        //    }
        //    catch {
        //        return null;
        //    }
        //}

        //private static void insertUser(UserInfoEntity entity)
        //{
        //    string value = Serializer<UserInfoEntity>(entity);
        //    string key = "User:" + entity.UserCode;
        //    StackExchangeRedisHelper.StringSet(Config.RedisDBNo, key, value);
        //}

        #region json序列化和反序列化
        private static string Serializer<T>(T entity)
        {
            string strJson = "";
            try
            {
                JavaScriptSerializer json = new JavaScriptSerializer();
                strJson = json.Serialize(entity);
            }
            catch (Exception ex)
            {
                return "";
            }
            return strJson;
        }
        private static T Deserialize<T>(string json)
        {
            T entity;
            try
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                entity = jss.Deserialize<T>(json);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return entity;
        }
        //public static string Serializer<T>(List<T> list)
        //{
        //    string strJson = "";
        //    try
        //    {
        //        JavaScriptSerializer json = new JavaScriptSerializer();
        //        strJson = json.Serialize(list);
        //    }
        //    catch (Exception ex)
        //    {
        //        return "";
        //    }
        //    return strJson;
        //}

        //public static List<T> Deserialize<T>(string json)
        //{
        //    List<T> list = null;
        //    try
        //    {
        //        JavaScriptSerializer jss = new JavaScriptSerializer();
        //        list = jss.Deserialize<List<T>>(json);

        //    }
        //    catch (Exception)
        //    {
        //        return list;
        //    }
        //    return list;
        //}
        #endregion
    }
}
